package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Coalprice;
import com.tianxin.workbao.workBao.bean.CtCoaltype;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.dataProvide.CoalPriceProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.SPUtils;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by hacker on 2017/7/26.
 */

public class AddCoalPriceActivity extends BaseActivity implements View.OnClickListener, UpdateListViewListener {
    private FloatingActionButton saveFab;
    private EditText unitPriceET;
    private TextView currCoalTV;
    private TextView startTimeTV;

    private Coalprice mCoalprice;
    private CtCoaltype mCtCoaltype;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_coal_price_option);
        super.initStatusBar(true);

        initData();
        initView();
        initEvent();
        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle(getResources().getString(R.string.add_oil));
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        saveFab = (FloatingActionButton) findViewById(R.id.save_fab);
        unitPriceET = (EditText) findViewById(R.id.unit_price);
        currCoalTV = (TextView) findViewById(R.id.curr_coal_name);
        startTimeTV = (TextView) findViewById(R.id.effect_time);
    }

    @Override
    public void initData() {
        mCoalprice = new Coalprice();
        Bundle bundle = getIntent().getExtras();
        mCtCoaltype = (CtCoaltype) bundle.getSerializable("ctCoalType");
        mCoalprice.setAdduserid((int) SPUtils.getPrefLong(Constant.CURRENT_USER_ID, 0));
        mCoalprice.setAddusername(SPUtils.getPrefString(Constant.CURRENT_USER_NAME, "0"));
        mCoalprice.setStatus(1);
        mCoalprice.setCoaltypeid(mCtCoaltype.getCoaltypeid());
        mCoalprice.setCoaltypename(mCtCoaltype.getCoaltypename());
        mCoalprice.setContractid(mCtCoaltype.getContractid());
    }


    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AddCoalPriceActivity.this.finish();
            }
        });
        saveFab.setOnClickListener(this);
        startTimeTV.setOnClickListener(this);
    }

    @Override
    public void bindView() {
        currCoalTV.setText(currCoalTV.getText() + " " + mCtCoaltype.getCoaltypename());
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.save_fab:
                addCoalPrice();
                break;
            case R.id.effect_time:
                getTimePickerDialog(new OnDateSetListener() {
                    @Override
                    public void onDateSet(com.jzxiang.pickerview.TimePickerDialog timePickerView, long millseconds) {
                        startTimeTV.setText(TimeUtils.parseDateTime(new Date(millseconds)));
                    }
                }).show(getSupportFragmentManager(), "all");
                break;
        }
    }

    /**
     * add Coal Price
     */
    private void addCoalPrice() {
        Float price = Float.valueOf(0);
        try {
            price = Float.valueOf(unitPriceET.getText().toString());
        } catch (Exception e) {
            showMessage("价格输入错误！");
            unitPriceET.setText("0");
            unitPriceET.setFocusable(true);
            return;
        }
        mCoalprice.setUnitprice(price);
        Date effectDate;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            effectDate = sdf.parse(startTimeTV.getText().toString());
        } catch (Exception e) {
            showMessage("时间选择错误！");
            startTimeTV.setText("点击选择生效时间！");
            startTimeTV.setFocusable(true);
            return;
        }
        mCoalprice.setStarttime(effectDate);
        mCoalprice.setRemark(mCoalprice.getAddusername()
                + " 在 " + startTimeTV.getText().toString() + " 调价");
        mCoalprice.setIsis(1);
        mCoalprice.setAddtime(new Date());
        getCoalPriceProvide().save(mCoalprice);
    }


    @Override
    public void showMessage(String msg) {
        if (msg == "ok") {
            this.finish();
        } else if (msg.contains("failure")) {
            ToastUtils.showLong("调价失败！\n " +
                    msg.substring(msg.indexOf("failure") + 1, msg.length()));
        } else {
            ToastUtils.showShort(msg);
        }
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("", 1200);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {

    }

    @Override
    public void onLoadMoreData(Object result) {

    }

    public CoalPriceProvide getCoalPriceProvide() {
        return new CoalPriceProvide(this, this);
    }
}
